/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cliente;

import aso.upna.cancion.AbstractCancion;
import aso.upna.cancion.Metadata;
import aso.upna.playlist.GenericIterator;
import aso.upna.playlist.GenericList;
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

/**
 *
 * @author triki
 */

public class Search {

    public static GenericList search_by_pattern(ConcurrentHashMap map,String pt)
    {
        GenericList lista = new GenericList();

        if(pt.startsWith("*")) //Caracter especial. Indica mostrar TODO.
        {
            Collection col = map.values();
            Iterator iter = col.iterator();

            System.out.println("Buscando...");
            int i = 1;
            while(iter.hasNext())
            {
                System.out.println("num lista:"+i);
                GenericList aux = (GenericList) iter.next();
                lista = GenericList.mergeLists(lista, aux);
                System.out.println("elementos: "+lista.getCountElements());
                i++;
            }
            
            return lista;
        }

        if(pt.length() > 0)
        {
            Collection col = map.values();
            Iterator iter = col.iterator();

            while(iter.hasNext())
            {
                GenericList aux = (GenericList) iter.next();
                GenericIterator g_iter = aux.getIterator();

                while(g_iter.hasNext())
                {
                    AbstractCancion can = (AbstractCancion) g_iter.current();

                    String cadena = can.getFilename();

                    if(cadena.contains(pt.subSequence(0, pt.length())))
                    {
                        lista.addElement(can);
                        g_iter.next();
                        continue;
                    }

                    if(can.hasMetadata())
                    {
                        Metadata m_data = can.getMetadatos();

                        String titulo = m_data.getTitulo();
                        String grupo = m_data.getGrupo();
                        String album = m_data.getAlbum();

                        if(titulo.contains(pt.subSequence(0, pt.length())))
                        {
                            lista.addElement(can);
                            g_iter.next();
                            continue;
                        }

                        if(grupo.contains(pt.subSequence(0, pt.length())))
                        {
                            lista.addElement(can);
                            g_iter.next();
                            continue;
                        }

                        if(album.contains(pt.subSequence(0, pt.length())))
                        {
                            lista.addElement(can);
                            g_iter.next();
                            continue;
                        }
                    }
                    g_iter.next();
                }
            }
            System.out.println("elementos!: "+lista.getCountElements());
            return lista;
        }
        
        return null;
    }
}
